package com.lh.qt.test;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpUtil;
import com.lh.qt.entity.Info;
import com.lh.qt.entity.Trade;
import com.lh.qt.entity.TradeInfo;
import com.lh.qt.service.InfoService;
import com.lh.qt.service.TradeInfoService;
import com.lh.qt.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Random;

/**
 * Author :李雪 2023/11/28
 * Department : 研发部
 */
@Component
public class Start {
    @Autowired
    InfoService infoService;

    @Autowired
    TradeService tradeService;

    @Autowired
    TradeInfoService tradeInfoService;
    public void init() {

        DecimalFormat df = new DecimalFormat("300000");


        for (int i = 1; i < 10000; i++) {

            String formatted = df.format(i);

            String result =  HttpUtil.get("https://qt.gtimg.cn/q=sz"+formatted);
            ThreadUtil.safeSleep(3000);
            if(result.contains("v_pv_none_match")){
                continue;
            }

            Info info1 = new Info();
            try {
                String[] info = result.split("=")[1].split("~");
                Field[] fileds = Info.class.getDeclaredFields();
                for (int k = 0; k < fileds.length-1; k++) {
                    Field field = fileds[k];
                    field.setAccessible(true);
                    field.set(info1,info[k+1]);
                }
                info1.setId(Integer.parseInt(info1.getCode()));
                infoService.save(info1);
            }catch ( Exception e){
                System.out.println(e.getMessage());
            }
        }

    }
    @PostConstruct
    public void init1(){



        List<Info> infos = infoService.list();
        for (Info info1 : infos) {
            String code = String.valueOf(info1.getId());
            if(info1.getId()<10000){
                DecimalFormat df = new DecimalFormat("000000");
                code = df.format(info1.getId());
            }
            String result = "";
            if(code.startsWith("6")){

                result =  HttpUtil.get( "https://web.ifzq.gtimg.cn/other/klineweb/klineWeb/weekTrends?code=sh"+code+"&type=qfq&_var=trend_qfq");
            }else{
                result =  HttpUtil.get( "https://web.ifzq.gtimg.cn/other/klineweb/klineWeb/weekTrends?code=sz"+code+"&type=qfq&_var=trend_qfq");
            }

            result =  result.split("\\=")[1];
            ThreadUtil.safeSleep(500);
            //https://proxy.finance.qq.com/ifzqgtimg/appstock/app/newfqkline/get?_var=kline_dayqfq1999&param=sz000001,day,1999-01-01,1999-12-31,320,qfq&r=0.7179903462067043
            TradeInfo tradeInfo = new TradeInfo();
            tradeInfo.setId(info1.getId());
            tradeInfo.setStr(result);
            tradeInfoService.saveOrUpdate(tradeInfo);
        }
    }

    private String zeroFill(String arg) {
        if (arg != null && arg.length() < 5) {
            StringBuilder tmpStr = new StringBuilder();
            for (int i = 0,length = 8 - arg.length(); i < length; i++) {
                tmpStr.append(" ");
            }
            return tmpStr.append(arg).toString();
        }

        return arg;
    }
}
